/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.magri.mvc.pesquisa.tela;

import org.magri.modulo2.agenda.bean.Cidade;
import org.magri.modulo2.agenda.dao.CidadeDAO;
import org.magri.mvc.pesquisa.controler.PesquisaControler;
import org.magri.mvc.pesquisa.modelo.Tabela;

/**
 *
 * @author Magri
 */
public class PesquisaTela extends javax.swing.JDialog {
    PesquisaControler pc;
    String sql;
    
    /**
     * Creates new form PesquisaTela
     */
    public PesquisaTela(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    public PesquisaTela(java.awt.Frame object, boolean b, String sql) {
        this(object, b);
        
        pc = new PesquisaControler();
        this.sql = sql;
        
        executarPesquisa(null);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jtfFiltroPesquisa = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Consulta");

        jPanel1.setLayout(new java.awt.BorderLayout());

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Pesquisa"));
        jPanel3.setLayout(new java.awt.BorderLayout());

        jtfFiltroPesquisa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtfFiltroPesquisaActionPerformed(evt);
            }
        });
        jPanel3.add(jtfFiltroPesquisa, java.awt.BorderLayout.CENTER);

        jPanel1.add(jPanel3, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel1, java.awt.BorderLayout.NORTH);

        jPanel2.setLayout(new java.awt.BorderLayout());

        jTable1.setModel(getDefaultTableModel());
        jTable1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jTable1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTable1KeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jPanel2.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jtfFiltroPesquisaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtfFiltroPesquisaActionPerformed
        executarPesquisa(jtfFiltroPesquisa.getText());
    }//GEN-LAST:event_jtfFiltroPesquisaActionPerformed

    private void jTable1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTable1KeyPressed
        if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_ENTER) {
            dispose();
        }
    }//GEN-LAST:event_jTable1KeyPressed

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        if (evt.getClickCount() > 1) {
            dispose();
        }
    }//GEN-LAST:event_jTable1MouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PesquisaTela.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PesquisaTela.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PesquisaTela.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PesquisaTela.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the dialog
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                PesquisaTela dialog = new PesquisaTela(new javax.swing.JFrame(), true, new CidadeDAO().getSqlPesquisa());
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    
    private javax.swing.table.DefaultTableModel getDefaultTableModel() {
        // colocar aki a inicializacao do DefaultTableModel
        
        dTableModel = new javax.swing.table.DefaultTableModel() {

            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
            
        };
        
        return dTableModel;
    }
    
    
    private void executarPesquisa(String filtro) {
        pesquisa = pc.pesquisa(sql, filtro);
        
        dTableModel.setDataVector(pesquisa.getLinhas(), pesquisa.getColunas());
        
        jTable1.updateUI();
    }
    
    public Integer getSelected(int index) {
        
        int selRow = jTable1.getSelectedRow();
        
        return (Integer) pesquisa.getLinhas().get(selRow).get(index);
    }
    
    private javax.swing.table.DefaultTableModel dTableModel;
    private Tabela pesquisa;
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jtfFiltroPesquisa;
    // End of variables declaration//GEN-END:variables
}
